<?php
/**
 * This Class encalsulates all possible Actions on a specific module. Mostly the functions beeing 
 * called by the PageController, but they should be accessible without a Page controller so you 
 * can use the methods of a module in other modules too.
 * 
 * When you implement a module based on this class, you have to implement/override the following
 * functions, methods or variables:
 * - $moduleName
 * 
 * @author michi
 *
 */
abstract class eCMSModule {
 	
	/**
	 * The Filesystem-Path to the ModuleFile
	 * @var string 
	 */
	
	private $moduleFilePath;
	
 	/**
 	 * This is the InstanceId of the Module as Speficied in the Database-Table tblModuleInstance
 	 * @var int
 	 */
 	private $fkModuleInstance;
 	
 	/**
 	 * This is the Name of the Module and should be overriden in your specific module
 	 * @var string
 	 */
 	protected $moduleName;
 	
 	/**
 	 * @var Default is 1, means the normal Page Class, if 2 -> ModuleControllerPage
 	 */
 	protected $moduleControllerVersion = 1;
 	
 	
 	/********************************
 	 *        PUBLIC METHODS        *
 	 *        ==============        *
 	 ********************************/
 	
 	public function getModuleName() {
 		return $this->moduleName;
 	} 
 	
 	/**
 	 * This Method sets the ModuleInstanceId
 	 * @param $fkModuleInstance int
 	 * @return void
 	 */
 	public final function setModuleInstance($fkModuleInstance) {
 		$this->fkModuleInstance = $fkModuleInstance;
 	}

 	/**
 	 * This method is called from the ModuleManager when it initalizes the Module, it sets 
 	 * the Path in wich the Module has been found
 	 * @param $path The Full FilePath to the Module
 	 * @return void
 	 */
 	public final function setModuleFilePath($moduleFilePath) {
 		$this->moduleFilePath = $moduleFilePath;
 	}
 	
 	/**
 	 * Returns the Version Id of the PageController-Class
 	 * @return unknown_type
 	 */
 	public final function getModuleControllerVersion() {
 		return $this->moduleControllerVersion;
 	}
 	
 	/** This method returns the PageController of the Module 
 	 * 
 	 * @return Page
 	 */

 	public final function getPageController($pkPage=null) {
 		
 		//try to find the file based on the modulePath
 		$dir = dirname($this->moduleFilePath);
 		
 		if($this->moduleControllerVersion==1) {
	 		if(substr($this->moduleName, -4)!="Page") {
	 			$filename = $dir . "/controller/" . $this->moduleName . "Page.class.php";
	 		}
	 		else {
	 			$filename = $dir . "/controller/" . $this->moduleName . ".class.php";
	  		}
	  		
	  		$className = $this->moduleName;
 		}
 		elseif($this->moduleControllerVersion==2) {
 			$filename = $dir . "/controller/" . $this->moduleName . "Controller.class.php";
 			$className = $this->moduleName."Controller";
 		} 
 		 		 		
 		if(is_file($filename)) {

 			require_once($filename);
 			$page = new $className($pkPage);
 			
 			return $page;
 			
 		}
 		else {
 			echo "file: $filename not found";
 			throw new eCMSException("PageController '$filename' not found!", "PageControllerMissing");
 		}
 		
 	}
 	
 }